package org.chnxi.study.datastructrues.tree;

/**
 * 线索化二叉树示例
 */
public class ThreadedBinaryTreeDemo {

    public static void main(String[] args) {

        ThreadedBinaryTree<Person> tbt = createTree();
        TBTreeNode<Person> tbtn5 = tbt.searchByInfixOrder(new Person(10 , "eee"));
        System.out.println("中序遍历情况：");
        tbt.infixOrder();
        System.out.println("--------------------------------------------------");
        //使用中序遍历方式对二叉树进行线索化
        tbt.threadedNodesByInfixOrder();

        //线索化结果测试
        System.out.println("验证中序线索化结果：");
        System.out.println("No=10节点的前驱情况："+tbtn5.getLeft());
        System.out.println("No=10节点的后继情况："+tbtn5.getRight());

        //遍历输出
        System.out.println("验证中序线索化遍历：");
        tbt.printThreadByInfixOrder();
        System.out.println("==================================================");


        tbt = createTree();
        tbtn5 = tbt.searchByInfixOrder(new Person(10 , "eee"));
        System.out.println("前序遍历情况：");
        tbt.preOrder();
        System.out.println("--------------------------------------------------");
        tbt.threadedNodesByPreOrder();
        //线索化结果测试
        System.out.println("验证中序线索化结果：");
        System.out.println("No=10节点的前驱情况："+tbtn5.getLeft());
        System.out.println("No=10节点的后继情况："+tbtn5.getRight());

        //遍历输出
        System.out.println("验证前序线索化遍历：");
        tbt.printThreadedTreeByPreOrder();
        System.out.println("==================================================");

        tbt = createTree();
        tbtn5 = tbt.searchByInfixOrder(new Person(10 , "eee"));
        System.out.println("后续遍历情况：");
        tbt.postOrder();
        System.out.println("--------------------------------------------------");
        tbt.threadedNodesByPostOrder();
        //线索化结果测试
        System.out.println("验证后续线索化结果：");
        System.out.println("No=10节点的前驱情况："+tbtn5.getLeft());
        System.out.println("No=10节点的后继情况："+tbtn5.getRight());

        System.out.println("验证后续线索化遍历：");
        tbt.printThreadedByPostOrder();
        System.out.println("==================================================");

    }

    /**
     * 手动创建树
     * @return
     */
    public static ThreadedBinaryTree<Person> createTree(){
        //初始化节点
        TBTreeNode<Person> root = new TBTreeNode<Person>(new Person(1 , "aaa"));
        TBTreeNode<Person> tbtn2 = new TBTreeNode<Person>(new Person(3 , "bbb"));
        TBTreeNode<Person> tbtn3 = new TBTreeNode<Person>(new Person(6 , "ccc"));
        TBTreeNode<Person> tbtn4 = new TBTreeNode<Person>(new Person(8 , "ddd"));
        TBTreeNode<Person> tbtn5 = new TBTreeNode<Person>(new Person(10 , "eee"));
        TBTreeNode<Person> tbtn6 = new TBTreeNode<Person>(new Person(14 , "fff"));

        //手动创建二叉树结构
        root.setLeft(tbtn2);
        root.setRight(tbtn3);
        tbtn2.setLeft(tbtn4);
        tbtn2.setRight(tbtn5);
        tbtn3.setLeft(tbtn6);
        ThreadedBinaryTree<Person> tbt = new ThreadedBinaryTree<>();
        tbt.setRoot(root);
        return tbt;
    }

}

